/**
 * Handles Agent Creation, and sends agents to the platform to be sent, also handles agents
 * to be received.
 * @author jon
 */

package manager;

import java.util.Vector;
import ui.IdleScreen;
import ap.NoSuchElementException;
import agent.*;

public class IOHandlerManager implements Runnable {
	private ap.AgentPlatform agentPlatform;
	private AgentManagerMIDlet midlet;
	private boolean isAgentWaiting;
	
//	SocketConnection.SocketConnectionServer server;
	
	public IOHandlerManager(AgentManagerMIDlet midlet,String serverAddress){
		this.midlet = midlet;
		isAgentWaiting = false;
		serverAddress = serverAddress.substring(10);//
		String UDPaddress = "";
		int UDPaddress2;
		UDPaddress2 = 10 + Integer.parseInt(serverAddress);
		UDPaddress += UDPaddress2;
		
		System.err.println("Creating AP, UDP Address is: " + UDPaddress + " TCP Address is: " + serverAddress);
		agentPlatform = new ap.AgentPlatform(this, UDPaddress, serverAddress);
//		this.server = new SocketConnection.SocketConnectionServer(serverAddress);
//		this.server.start();
	}
	
	public boolean isAgentWaiting(){
		return isAgentWaiting;
	}
	
	public void agentWaiting(){
		this.isAgentWaiting = true;
	}
	
	public void run() {
		//agentPlatform.run();
	}

	public Object[] vecToArray(Vector v){
		Object[] array = new String[v.size()];
		for(int i = 0 ; i < array.length ; i++){
			array[i] = v.elementAt(i);
		}
		return array;
	}
	
	public void createMeetingAgent(Vector dests, 
									Vector startTimes,
									Vector endTimes,
									int meetingLength,
									String connectionType){
		int numDestinations = dests.size();
		int numTimes = startTimes.size();
		boolean[][] rTimes = new boolean[numDestinations][numTimes];
		System.out.println("Before first cast");
		String[] destinations = (String[])vecToArray(dests);
		System.out.println("After first cast");
		String[] meetingStartTimes = (String[])vecToArray(startTimes);
		System.out.println("After second cast");
		String[] meetingEndTimes = (String[])vecToArray(endTimes);
		System.out.println("After third cast");
		
		//create agent
//		agent.MeetingAgent agent = new agent.MeetingAgent( meetingStartTimes,
//												meetingEndTimes,
//												dests.size(),
//												destinations,
//												0,
//												rTimes);
		
		//Create agent and set properties
	//	server.setUpClient(destinations[0]);
		System.err.println("After setting up client");
		MeetingAgent agent;
		System.err.println("After declaration");
		agent = new MeetingAgent();
		System.err.println("After constructor");
		if(connectionType.equals("D")){
			agent.setConnectionDatagram();
		}else if(connectionType.equals("S")){
			agent.setConnectionSocket();
		}
		System.err.println("After setting connection type");
		for(int i = 0 ; i < destinations.length ; i++)
			agent.addContact("tempcontactname" + i, destinations[i]);
		//agent.addContact("me", server.getAddress());
		System.err.println("after setting destinations");
		for(int i = 0 ; i < meetingStartTimes.length ; i++)
			agent.addTime(meetingStartTimes[i], meetingEndTimes[i]);
		System.err.println("After setting times");
		//send agent as string to first destination
		System.out.println(agent.toString());
		//server.sendMessage(destinations[0], agent.toString());
	//	server.setUpClient(destinations[0]);
	//	System.err.println("After setting up client");
//		server.sendMessageViaClient(agent.toString());
	//	System.err.println("After sending message");
	//	agentPlatform.setOutAgent(agent);
		//agentPlatform.setAgent(agent); //add agent to agentPlatform's outgoing queue
		this.sendMeetingAgent(agent);
		midlet.getUIHandlerManager().setIdleScreen();
	}
	
	public void sendMeetingAgent(MeetingAgent agent){
		agentPlatform.setOutAgent(agent);
	//	server.setUpClient(agent.getAddresses(agent.getNextIndex()));
	//	System.out.println("agent.getNextIndex() is : " + agent.getNextIndex());
	//	System.out.println("agent.getAddresses(agent.getNextIndex()) is :" + agent.getAddresses(agent.getNextIndex()));
//		server.setUpClient(agent.getAddresses(agent.getNextIndex()));
	//	server.sendMessageViaClient(agent.toString());
	}
	
	/*Receive an agent via the agent Platform */
	public MeetingAgent receiveAgent(){
		//String temp;
		MeetingAgent agent = null;
		try{
			agent = agentPlatform.getInAgent();
		}catch(Exception e){
			e.printStackTrace();
		}
		return agent;
		//agentPlatform.getInAgent()
//		if((temp=server.getAgent())!=null)
//			return temp;
//		else
//			return null;
			
	}
}
